package com.handinfo.android.uicontrols.controls;


import android.graphics.Bitmap;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.utils.Tools;

/**
 * 选择控件
 * @author handinfo
 */
public class DWCheckbox extends DWControl
{

	private boolean m_is_checked = false;//是否被选中

	private Bitmap img_rect = null;
	private Bitmap img_checked = null;
	public DWCheckbox()
	{
		img_rect = DWControlsManager.getInstance().createControlImage("dengluxz_1");
		img_checked = DWControlsManager.getInstance().createControlImage("dengluxz_2");
		setShowWideHigh(67, 42);
	}

	public void render(DWGraphics g)
	{
		g.setClip(m_show_x, m_show_y, m_show_w, m_show_h);
		g.drawBitmap(img_rect, m_show_x + m_show_w / 2 - 10, m_show_y + m_show_h / 2, Tools.HCENTER_VCENTER);
		if (m_is_checked) {
			g.drawBitmap(img_checked, m_show_x + m_show_w / 2, m_show_y + m_show_h / 2, Tools.HCENTER_VCENTER);
		}
		renderRect(g);
	}

	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		setSubClip(g, x, y, w, h, offsetx, offsety);
		g.drawBitmap(img_rect, x + m_show_x + m_show_w / 2 - offsetx, y + m_show_y + m_show_h / 2 - offsety, Tools.HCENTER_VCENTER);
		if (m_is_checked) {
			g.drawBitmap(img_checked, x + m_show_x + m_show_w / 2 - offsetx, y + m_show_y + m_show_h / 2 - offsety, Tools.HCENTER_VCENTER);
		}
		renderLimitsRect(g, x, y, offsetx, offsety);
	}

	public void refreshControl()
	{
	}
	/**
	 * 是否被选中
	 * @return
	 */
	public boolean getChecked()
	{
		return m_is_checked;
	}

	/**
	 * 设置选中状态
	 * @param checked
	 */
	public void setChecked(boolean checked)
	{
		m_is_checked = checked;
	}

	public void logic()
	{

	}

	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			m_is_checked = !m_is_checked;
			if (m_listener != null) {
				m_listener.OnClick();
			}
			return true;
		}
		return false;
	}

}
